﻿using JNPF.Basics.Logic.DataInterface;
using JNPF.Basics.Models.DataInterface.Entities;
using JNPF.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic.Common.Login
{
    /// <summary>
    /// 数据接口
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2017.11.07
    /// </summary>
    public class DataInterfaceBll
    {
        private DataInterfaceIService service = new DataInterfaceService();
        private DbLinkIService dbLinkservice = new DbLinkService();
        private DatabaseIService databaseService = new DatabaseService();
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">参数</param>
        /// <returns></returns>
        public async Task<PageResult<DataInterfaceEntity>> GetList(PageInput input)
        {
            try
            {
                return await service.GetList(input);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 列表下拉框
        /// </summary>
        /// <returns></returns>
        public async Task<List<DataInterfaceEntity>> GetSelector()
        {
            try
            {
                return await service.GetList();
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        public async Task<DataInterfaceEntity> GetInfo(string id)
        {
            try
            {
                return await service.GetInfo(id);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public async Task<int> Create(DataInterfaceEntity entity)
        {
            try
            {
                entity.Id = CommonHelper.Guid;
                entity.CreatorTime = DateTime.Now;
                entity.CreatorUserId = UserProvider.Instance.Get().UserId;
                return await service.Create(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public async Task<int> Delete(DataInterfaceEntity entity)
        {
            try
            {
                return await service.Delete(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id">主键id</param>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public async Task<int> Update(string id, DataInterfaceEntity entity)
        {
            try
            {
                entity.Id = id;
                entity.LastModifyTime = DateTime.Now;
                entity.LastModifyUserId = UserProvider.Instance.Get().UserId;
                return await service.Update(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="id"></param>
        /// <param name="sql"></param>
        /// <returns></returns>
        public async Task<DataTable> GetData(string id, string sql)
        {
            var entity = await service.GetInfo(id);
            var result = await connection(entity.DBLinkId, sql);
            return result;
        }

        public async Task<DataTable> GetData(string id)
        {
            var data = await service.GetInfo(id);
            var result = await connection(data.DBLinkId, data.Query);
            return result;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="id"></param>
        /// <param name="sql"></param>
        /// <returns></returns>
        public async Task<DataTable> connection(string dbLinkId, string sql)
        {
            var linkEntity = await dbLinkservice.GetInfo(dbLinkId);
            var dt = await databaseService.GetInterFaceData(linkEntity, sql);
            return dt;
        }
    }
}
